import requests
import json


class DingTalk_Base:
    def __init__(self):
        self.__headers = {'Content-Type': 'application/json;charset=utf-8'}
        self.url = ''

    def send_msg(self, text):
        text = '爬虫错误日志:' + text
        json_text = {
            "msgtype": "markdown",
            "markdown": {"title": "爬虫日志",
                         "text": text
                         },
            "at": {
                "isAtAll": False
            }
        }
        # proxies = {
        #     "http": "http://127.0.0.1:8888",
        #     "https": "http://127.0.0.1:8888"
        # }
        # return requests.post(self.url, json.dumps(json_text), headers=self.__headers, proxies=proxies, verify=False).content
        return requests.post(self.url, json.dumps(json_text), headers=self.__headers).content


class DingTalk_Disaster(DingTalk_Base):
    def __init__(self):
        super().__init__()
        # 填写机器人的url
        self.url = 'https://oapi.dingtalk.com/robot/send?access_token' \
                   '=1e1469ec18c17cbcfc0cdf0ca24785493726ddb214e1da3932e09a4d685c3b12 '


if __name__ == '__main__':
    ding = DingTalk_Disaster()
    res = ding.send_msg('爬虫日志')
